/*
 * Copyright (C) 2015-2020, Wazuh Inc.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdio.h>

#include "../../wazuh_modules/wmodules.h"
#include "../../headers/shared.h"

extern int test_mode;
extern char **d_sub_strings;

/* redefinitons/wrapping */

int __wrap_fclose(FILE *stream);

size_t __wrap_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

FILE *__wrap_fopen(const char * __filename, const char * __modes);

int __wrap_remove (const char *__filename);

int __wrap_fgets(char *__restrict __s, int __n, FILE *__restrict __stream);

char *__wrap_strerror (int __errnum);

void __wrap__merror(const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mterror(const char *tag, const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mtwarn(const char *tag, const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mterror_exit(const char *tag, const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mtdebug1(const char *tag, const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mtdebug2(const char *tag, const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mdebug1(const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mdebug2(const char * file, int line, const char * func, const char *msg, ...);

void __wrap__mdebug1(const char *file, int line, const char *func, const char *msg, ...);

int __wrap_OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS * settings);

int __wrap_OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings);

int __wrap_StartMQ(const char *key, short int type);

int __wrap_SendMSG(int queue, const char *message, const char *locmsg, char loc);

int __wrap_OS_ConnectUnixDomain(const char *path, int type, int max_msg_size);

int __wrap_OS_SendSecureTCP(int sock, uint32_t size, const void * msg);

int __wrap_OS_RecvSecureTCP(int sock, char * ret, uint32_t size);

int __wrap_close();

int __wrap_sqlite3_prepare_v2();

int __wrap_wm_vuldet_prepare();

int __wrap_sqlite3_bind_text(sqlite3_stmt* pStmt, int a, const char* b, int c, void *d);

int __wrap_sqlite3_bind_int(sqlite3_stmt* pStmt, int a, int b);

int __wrap_sqlite3_step();

int __wrap_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs);

int __wrap_bzip2_uncompress();

time_t __wrap_time(time_t *t);

struct tm *__wrap_localtime(const time_t *t);

int __wrap_sqlite3_column_int(sqlite3_stmt *pStmt, int i);

double __wrap_sqlite3_column_double(sqlite3_stmt *pStmt, int i);

const unsigned char *__wrap_sqlite3_column_text(sqlite3_stmt *pStmt, int i);

const unsigned char *__real_sqlite3_column_text(sqlite3_stmt *pStmt, int i);

int __wrap_sqlite3_close_v2();

int __wrap_sqlite3_finalize();

int __wrap_wdb_finalize();

const char *__wrap_sqlite3_errmsg();

cJSON * __wrap_cJSON_CreateObject(void);

cJSON * __real_cJSON_CreateObject(void);

cJSON * __wrap_cJSON_CreateArray(void);

cJSON * __real_cJSON_CreateArray(void);

cJSON * __wrap_cJSON_CreateNumber(double num);

cJSON * __wrap_cJSON_CreateString(const char *string);

cJSON * __real_cJSON_CreateString(const char *string);

void __wrap_cJSON_AddItemToArray(cJSON *array, cJSON *item);

void __real_cJSON_AddItemToArray(cJSON *array, cJSON *item);

void __wrap_cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);

void __real_cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);

cJSON * __wrap_cJSON_GetObjectItem(const cJSON * const object, const char * const string);

cJSON * __wrap_cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);

cJSON * __wrap_cJSON_Parse(const char *value);

cJSON * __real_cJSON_Parse(const char *value);

char * __wrap_cJSON_PrintUnformatted(const cJSON *item);

void __wrap_cJSON_Delete(cJSON *item);

void __real_cJSON_Delete(cJSON *item);

cJSON * __wrap_json_fread(const char * path, char retry);

OSHashNode *__wrap_OSHash_Begin(const OSHash *self, unsigned int *i);

OSHashNode *__wrap_OSHash_Next(const OSHash *self, unsigned int *i, OSHashNode *current);

int __wrap_OSHash_Create();

int __wrap_OSHash_setSize(OSHash *self, unsigned int new_size);

void *OSHash_Clean(OSHash *self, void (*cleaner)(void*));

int __wrap_OSHash_Add(OSHash *self, const char *key, void *data);

void *__wrap_OSHash_Get(const OSHash *self, const char *key);

int __wrap_wstr_end(char *str, const char *str_end);

int __wrap_w_uncompress_gzfile(const char *gzfilesrc, const char *gzfiledst);

int __wrap_wurl_request(const char * url, const char * dest, const char *header, const char *data);

unsigned int __wrap_sleep (unsigned int __seconds);

extern int __real_OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);

int __wrap_OSRegex_Compile(const char *pattern, OSRegex *reg, int flags);

extern const char *__real_OSRegex_Execute(const char *str, OSRegex *reg);

const char *__wrap_OSRegex_Execute(const char *str, OSRegex *reg);

// wm_vuln_detector mocks

int __wrap_wm_checks_package_vulnerability(char *version_a, const char *operation, const char *version_b, version_type vertype);

int __wrap_wm_vuldet_win_nvd_vulnerabilities(sqlite3 *db, agent_software *agent, wm_vuldet_flags *flags);

int __wrap_wm_vuldet_linux_nvd_vulnerabilities(sqlite3 *db, agent_software *agent, OSHash *cve_table);

int __wrap_OS_ReadXML(const char *file, OS_XML *_lxml);

xml_node **__wrap_OS_GetElementsbyNode(const OS_XML *_lxml, const xml_node *node);

char * __wrap_w_get_file_content(const char * path, int max_size);

int __wrap_wm_vuldet_json_nvd_parser(char *json_feed, wm_vuldet_db *parsed_vulnerabilities);

int __wrap_wm_vuldet_json_wcpe_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);

int __wrap_wm_vuldet_json_msu_parser(cJSON *json_feed, wm_vuldet_db *parsed_vulnerabilities);

int __wrap_opendir();

int __wrap_closedir();

struct dirent * __wrap_readdir();

int __wrap_w_is_file(const char * const file);

// wm_vuln_detector_evr mocks

bool __wrap_pkg_version_relate(const struct pkg_version *a, enum pkg_relation rel, const struct pkg_version *b, version_type vertype);

bool __wrap_c_isdigit(int c);

//wm_vuln_detector_nvd

int __wrap_wm_vuldet_add_cve_node(cve_vuln_pkg *newPkg, const char *cve, OSHash *cve_table);

void __wrap_wm_vuldet_free_cve_node(cve_vuln_pkg *newPkg);
